package com.ruoyi.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.domain.SysRole;
import com.ruoyi.domain.SysUser;
import com.ruoyi.mapper.SysRoleMapper;
import com.ruoyi.mapper.SysUserMapper;

@Service
public class SysUserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	//private SysUserRoleMapper sysUserRoleMapper;
	
	@Autowired
	SysRoleMapper sysRoleMapper;
	
	public SysUser selectUserByUserName(String username) {
		return sysUserMapper.selectUserByUserName(username);
	}

	public List<SysUser> selectUserList(SysUser user) {
		return sysUserMapper.selectUserList(user);
	}

	public int insertUser(SysUser user) {
		return sysUserMapper.insertUser(user);
	}

	public boolean checkUserNameUnique(SysUser user) {
		Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = sysUserMapper.checkUserNameUnique(user.getUserName());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}

	public boolean checkPhoneUnique(SysUser user) {
		Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = sysUserMapper.checkPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}

	public boolean checkEmailUnique(SysUser user) {
		Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = sysUserMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}

	public SysUser selectUserById(Long userId) {
		SysUser sysUser = sysUserMapper.selectUserById(userId);
		
		List<SysRole> roles = sysRoleMapper.selectRoleListByUserId(userId);
		sysUser.setRoles(roles);
		
		return sysUser;
	}

	public void checkUserAllowed(SysUser user) {
		if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
            throw new ServiceException("不允许操作超级管理员用户");
        }
	}

	public int updateUser(SysUser user) {
		return sysUserMapper.updateUser(user);
	}

	public void checkUserDataScope(Long userId) {
		if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            SysUser user = new SysUser();
            user.setUserId(userId);
            List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
            if (StringUtils.isEmpty(users)) {
                throw new ServiceException("没有权限访问用户数据！");
            }
        }
	}
	
	
	

}
